package cn.zb.zbrhback.auth.controller;

import cn.zb.zbrhback.auth.annotation.ZBPermission;
import cn.zb.zbrhback.auth.domain.Menu;
import cn.zb.zbrhback.auth.domain.MenuQuery;
import cn.zb.zbrhback.auth.domain.Permission;
import cn.zb.zbrhback.auth.domain.PermissionQuery;
import cn.zb.zbrhback.auth.service.IMenuService;
import cn.zb.zbrhback.basic.util.AjaxResult;
import cn.zb.zbrhback.basic.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
@ZBPermission(name = "菜单管理",desc = "菜单管理")
@Api(value = "菜单Api",tags = {"菜单操作接口"})
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping
    @ApiOperation(value = "获取所有菜单",notes = "不需要参数")
    @ZBPermission(name = "获取所有菜单",desc = "获取所有菜单")
    public AjaxResult getAll(){
        try {
            List<Menu> menus = menuService.getAll();
            return AjaxResult.me().setData(menus);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("获取所有菜单失败"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 获取所有菜单，以树形展示用于设置菜单
     * */
    @GetMapping("/tree")
    @ApiOperation(value = "获取菜单树",notes = "不需要参数")
    @ZBPermission(name = "获取菜单树权限",desc = "获取菜单树权限")
    public AjaxResult getTree(){
        try {
            List<Menu> menus = menuService.getTree();
            return AjaxResult.me().setData(menus);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("获取菜单树失败"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 获取员工拥有的菜单，用于动态树形展示
     * */
    @GetMapping("/employee/tree/{eid}")
    @ApiOperation(value = "获取菜单树",notes = "不需要参数")
    @ZBPermission(name = "获取菜单树权限",desc = "获取菜单树权限")
    public AjaxResult getEmployeeMenu(@PathVariable("eid") Long eid){
        try {
            List<Menu> menus = menuService.getEmployeeMenu(eid);
            return AjaxResult.me().setData(menus);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("获取动态菜单失败"+e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/menuid")
    @ApiOperation(value = "获取菜单id",notes = "不需要参数")
    @ZBPermission(name = "获取菜单id权限",desc = "获取菜单id权限")
    public AjaxResult getId(){
        try {
            List<Long> ids = menuService.getId();
            return AjaxResult.me().setData(ids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("获取菜单id失败"+e.getMessage()).setSuccess(false);
        }
    }

    @PostMapping
    @ApiOperation(value = "分页条件查询菜单",notes = "参数为当前页，每页条数和查询条件，条件可空")
    @ZBPermission(name = "分页条件查询菜单权限",desc = "分页条件查询菜单权限")
    public AjaxResult pageAll(@RequestBody MenuQuery menuQuery){
        try {
            PageResult<Menu> menuPageResult = menuService.findPage(menuQuery);
            return AjaxResult.me().setData(menuPageResult);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("分页条件查询菜单失败"+e.getMessage()).setSuccess(false);
        }
    }

    @PutMapping
    @ApiOperation(value = "新增/修改菜单",notes = "有id为修改，没有为新增")
    @ZBPermission(name = "新增/修改菜单权限",desc = "新增/修改菜单权限")
    public AjaxResult addorUpdate(@RequestBody Menu menu){
        try {
            if(Objects.nonNull(menu.getId())){
                menuService.update(menu);
            }else {
                menuService.add(menu);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("新增/修改菜单失败"+e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/parentmenu")
    @ApiOperation(value = "获取所有一级菜单",notes = "不需要参数")
    @ZBPermission(name = "获取所有一级菜单",desc = "获取所有一级菜单")
    public AjaxResult getAllParentMenu(){
        try {
            List<Menu> menus = menuService.getAllParentMenu();
            return AjaxResult.me().setData(menus);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("获取所有一级菜单失败"+e.getMessage()).setSuccess(false);
        }
    }
}
